//
//  JSMSkinViewController.m
//  TakeYouTourTogether
//
//  Created by dllo on 15/12/4.
//  Copyright © 2015年 king. All rights reserved.
//

#import "JSMSkinViewController.h"
#import "JSMSkinCell.h"
@interface JSMSkinViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, strong)UITableView *tableView;
@property(nonatomic, strong)NSArray *imageArr;
@end

@implementation JSMSkinViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = LanguageTool(@"一键换肤");
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"<<" style:UIBarButtonItemStylePlain target:self action:@selector(backAction)];
    self.imageArr = @[@"skin1", @"skin2", @"back1111", @"skin3", @"4", @"5", @"6", @"7", @"8"];
    [self createTableView];
}
- (void)createTableView{
    self.tableView = [[BaseTableView alloc]initWithFrame:self.view.frame];
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).with.insets(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
}
- (void)backAction{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark 协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.imageArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *str = @"reuse";
    JSMSkinCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    if (!cell) {
        cell = [[JSMSkinCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
    }
    cell.skinImageView.image = [UIImage imageNamed:self.imageArr[indexPath.row]];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [[NSNotificationCenter defaultCenter]postNotificationName:@"changeColor" object:nil userInfo:@{@"name" : self.imageArr[indexPath.row]}];
    [[NSUserDefaults standardUserDefaults] setObject:self.imageArr[indexPath.row] forKey:@"skinImageName"];
    //移除之前的
    if (self.tabBarController.tabBar.subviews[1].class == [BaseImageView class]) {
        NSLog(@"YES");
        [self.tabBarController.tabBar.subviews[1] removeFromSuperview];
    }
    BaseImageView * mView=[[BaseImageView alloc] initWithFrame:CGRectMake(0, 0, self.tabBarController.tabBar.frame.size.width, self.tabBarController.tabBar.frame.size.height)];//tabbar的颜色
    mView.image = [UIImage imageNamed:self.imageArr[indexPath.row]];
    [self.tabBarController.tabBar insertSubview:mView atIndex:1];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
